package src.twice;

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Stack;

import static com.sun.xml.internal.fastinfoset.alphabet.BuiltInRestrictedAlphabets.table;

public class PropertiesFileTool extends JFrame {

    private JTextField textField;


    protected void do_button_actionPerformed(ActionEvent e){
        JFileChooser chooser = new JFileChooser();
        chooser.setFileFilter(new FileNameExtensionFilter("属性文件","properties"));
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        chooser.setMultiSelectionEnabled(false);
        int result = chooser.showOpenDialog(this);

        if(result == JFileChooser.APPROVE_OPTION){
            File selectFile = chooser.getSelectedFile();
            textField.setText(selectFile.getAbsolutePath());
            Properties properties = new Properties();
            FileReader reader = null;
            //DefaultTableModel model = (DefaultTableModel) table.getModel;

            try {
                reader = new FileReader(selectFile);
                properties.load(reader);
                Enumeration<?> keys = properties.propertyNames();
                while (keys.hasMoreElements()){
                    String key = (String) keys.nextElement();
                    String value = properties.getProperty(key);
                    //model.addRow(new String[]{key,value});
                }
            } catch (FileNotFoundException ex) {
                throw new RuntimeException(ex);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }finally {
                try {
                    reader.close();
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            }
        }
    }
}
